package com.qinggeng.project.system.service.impl;
import com.github.pagehelper.PageHelper;
import com.qinggeng.common.exception.CustomException;
import com.qinggeng.common.redenvelop.BigDecimalUtil;
import com.qinggeng.common.utils.CalendarAdjustUtils;
import com.qinggeng.common.utils.StringUtils;
import com.qinggeng.framework.config.thread.ThreadPoolExecutorUtil;
import com.qinggeng.project.api.dto.UserAccountConsumptionDTO;
import com.qinggeng.project.system.domain.UserAccountConsumptionMonth;
import com.qinggeng.project.system.domain.UserAccountConsumptionYear;
import com.qinggeng.project.system.dto.SysUserAccountConsumptionYearDTO;
import com.qinggeng.project.system.mapper.UserAccountConsumptionMonthMapper;
import com.qinggeng.project.system.mapper.UserAccountConsumptionYearMapper;
import com.qinggeng.project.system.service.IUserAccountService;
import com.qinggeng.project.system.service.UserAccountConsumptionMonthService;
import com.qinggeng.project.system.service.UserAccountConsumptionYearService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.Future;
import java.util.concurrent.ThreadPoolExecutor;

@Slf4j
@Service
public class UserAccountConsumptionYearServiceImpl implements UserAccountConsumptionYearService {
    @Resource
    private UserAccountConsumptionYearMapper userAccountConsumptionYearMapper;

    @Override
    public UserAccountConsumptionYear getUserAccountConsumptionYear(UserAccountConsumptionYear userAccountConsumptionYear) {
        Example example =new Example(UserAccountConsumptionYear.class);
        example.createCriteria()
                .andEqualTo("userId",userAccountConsumptionYear.getUserId())
                .andEqualTo("createYear",userAccountConsumptionYear.getCreateYear())
                .andEqualTo("status",0)
                .andEqualTo("delFlag",0);
        return getUserAccountConsumptionYear(example);
    }

    private  UserAccountConsumptionYear getUserAccountConsumptionYear(Example example) {
        return  userAccountConsumptionYearMapper.selectOneByExample(example);
    }
    @Override
    public boolean insertUserAccountConsumptionYear(UserAccountConsumptionYear userAccountConsumptionYear) {
        Long userId=userAccountConsumptionYear.getUserId();
        if (null ==userId){
            throw  new CustomException("userId 不能为空");
        }
        userAccountConsumptionYear.setCreateYear(CalendarAdjustUtils.getYearToStr());
        Date date =new Date();
        userAccountConsumptionYear.setCreateTime(date);
        userAccountConsumptionYear.setUpdateTime(date);
       return userAccountConsumptionYearMapper.insertSelective(userAccountConsumptionYear)>=1;
    }

    @Override
    public boolean checkUserAccountConsumptionYear(UserAccountConsumptionYear userAccountConsumptionYear) {
        UserAccountConsumptionYear year=getUserAccountConsumptionYear(userAccountConsumptionYear);
        if (null ==year){
            insertUserAccountConsumptionYear(userAccountConsumptionYear);
        }
        return true;
    }

    @Override
    public boolean addAccountConsumptionYear(UserAccountConsumptionYear userAccountConsumptionYear) {

        return userAccountConsumptionYearMapper.addUpdateYearMonthMoney(userAccountConsumptionYear)>=1;
    }

    @Override
    public List<UserAccountConsumptionYear> listSysUserAccountConsumptionYear(SysUserAccountConsumptionYearDTO dto) {
        PageHelper.startPage(dto.getPageNum(),dto.getPageSize());
        return userAccountConsumptionYearMapper.selectAll();
    }
}
